﻿Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
Public Class FormLH
    Dim objHWSetting As New clsHWSetting
    Private WithEvents UcDigitalKeyboard1 As ucDigitalKeyboard
    Dim objUser As New clsUser
    Dim timerLhj As Timer '任务步骤
    Dim timerLhj_interval As Integer = 3000 '时间间隔
    Dim objClsSysParam As New clsSysParam
    Dim objAgvRoute As New clsAgvRoute
    Dim objClsHWSetting As New clsHWSetting
    Dim objPgdMes As New clsPgdMes
    Dim objLhPgd As New clsLhPgd
    Dim objCPCK As New clsCPCK
    Dim objProdInfo As New clsProdInfo
    Dim objPgdMesWorkReport As New clsPgdMesWorkReport
    Dim objLhCkMes As New clsLhCkMes
    Dim objTaskTM As New clsTaskTM
    Dim dtLHJ As DataTable
    Dim dttemp As New DataTable
    Dim agv_website As String
    Dim dgvSelectedEquipId As String = "-1"
    Dim dgvSelectedIndex As Integer = -1
    Dim rb_checked_text As String = "2"
    Dim rb_checked_details_text As String = "6"
    Dim dtxl As New List(Of String)
    Dim lhjLists As String = String.Empty
    Dim httpHelper As New HttpHelper
    Dim objLog As New clsLog
    Dim objFlbInfo As New clsFLBinfo
    Dim flb_info As DataTable
    Dim s_id As String
    Dim dlno As String
    Dim warehousecodelh As String
    Private Sub FormLH_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.WindowState = FormWindowState.Maximized
        dttemp.Columns.Add("equip_id", GetType(String))
        dttemp.Columns.Add("status", GetType(String))
        dttemp.Columns.Add("status_name", GetType(String))
        dttemp.Columns.Add("prod_id", GetType(String))
        dttemp.Columns.Add("prod_spec_id", GetType(String))
        dttemp.Columns.Add("prod_spec_name", GetType(String))
        dttemp.Columns.Add("sys_id", GetType(String))
        dttemp.Columns.Add("lh_pgd_code", GetType(String))
        dttemp.Columns.Add("jp_type", GetType(String))
        dttemp.Columns.Add("type_value", GetType(String))
        dttemp.Columns.Add("oper_count", GetType(String))
        dttemp.Columns.Add("lhj_finish_count", GetType(String))
        dttemp.Columns.Add("lhj_finish_count_out", GetType(String))
        connectDB()
        getUserListByCJ()
        initAgvWebSite()
        initData()
        initDgv()
        initTimer()
    End Sub

    Private Sub connectDB()
        g_szInitFileName = My.Application.Info.DirectoryPath & "\Client_LHJ.ini"
        g_Server.szDrive = GetIniFile("SERVER", "DRIVE", "PostgreSQL ANSI", g_szInitFileName)
        g_Server.szIPadr = GetIniFile("SERVER", "IP", "", g_szInitFileName)
        g_Server.szDbn = GetIniFile("SERVER", "DBN", "ZHENANSF", g_szInitFileName)
        g_Server.szUid = GetIniFile("SERVER", "UID", "sa", g_szInitFileName)
        g_Server.szPas = GetIniFile("SERVER", "PAS", "sa", g_szInitFileName)
        g_Server.iOver = CInt(GetIniFile("SERVER", "OVER", "-1", g_szInitFileName))
        warehousecodelh = GetIniFile("WAREHOUSE", "warehouse_code_lh", "", g_szInitFileName)
        If False = g_fDBconnect And 0 = g_iDBconnResult Then
            If "" = g_Server.szIPadr Or "0.0.0.0" = g_Server.szIPadr Then
            Else
                g_fDBconnect = True

                'データベース接続
                With g_Server
                    g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    If 1 <> g_iDBconnResult Then
                        .szDrive = CStr(IIf("PostgreSQL" = .szDrive, "PostgreSQL ANSI", "PostgreSQL"))
                        g_iDBconnResult = DBacs.Connect(.szDrive, .szIPadr, .szDbn, .szUid, .szPas, .iOver)
                    End If
                End With
            End If
        End If
    End Sub

    Private Sub getUserListByCJ()
        Dim dtUser As DataTable = objUser.getUserListByCJ(3, rb_checked_text, rb_checked_details_text)
        cmbOpers.Properties.Items.Clear()
        For index = 0 To dtUser.Rows.Count - 1
            cmbOpers.Properties.Items.Add(dtUser.Rows(index)("user_name"))
        Next
        If cmbOpers.Properties.Items.Count > 0 Then
            cmbOpers.Properties.Items(0).CheckState = CheckState.Checked
            cmbOpers.Properties.DropDownRows = cmbOpers.Properties.Items.Count + 1
            cmbOpers.Properties.ShowButtons = False
            cmbOpers.Properties.ShowPopupCloseButton = False
        End If
    End Sub

    Private Sub getRbChechedText()
        If RadioButton4.Checked Then
            rb_checked_text = "2"
            rb_checked_details_text = "6"
        End If
        If RadioButton5.Checked Then
            rb_checked_text = "3"
            rb_checked_details_text = "8"
        End If
        If RadioButton6.Checked Then
            rb_checked_text = "4"
            rb_checked_details_text = "10"
        End If
        If RadioButton1.Checked Then
            rb_checked_text = "1"
            rb_checked_details_text = "12"
        End If
    End Sub

    Private Sub RadioButtonText_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton4.CheckedChanged, RadioButton5.CheckedChanged, RadioButton6.CheckedChanged, RadioButton1.CheckedChanged
        getRbChechedText()
        getUserListByCJ()
    End Sub

    Private Sub initTimer()
        timerLhj = New Timer
        timerLhj.Interval = timerLhj_interval
        AddHandler timerLhj.Tick, AddressOf timerLhj_Tick
        timerLhj.Start()
    End Sub
    Private Sub timerLhj_Tick(sender As Object, e As EventArgs)
        Try
            initDgv()
        Catch ex As Exception
            MsgBox(ex.ToString())
            timerLhj.Stop()
            MsgBox("与服务器断开连接，请检查网络状态。")
            End 'Application.Restart()
        End Try
    End Sub

    Private Sub initDgv()
        Dim dtCurrentLhj As DataTable = objClsHWSetting.selectAllLHJ(lhjLists)
        dtCurrentLhj.Columns.Add("status_name", GetType(String))
        If dtCurrentLhj.Rows.Count > 0 Then
            For Each row As DataRow In dtCurrentLhj.Rows
                If row("status") = "0" Then
                    row("status_name") = "空闲"
                End If
                If row("status") = "1" Then
                    row("status_name") = "忙"
                End If
                If row("status") = "2" Then
                    row("status_name") = "失联"
                End If
                If row("status") = "3" Then
                    row("status_name") = "要出炉"
                End If
                If row("status") = "4" Then
                    row("status_name") = "硫化异常"
                End If
                If row("status") = "5" Then
                    row("status_name") = "维修"
                End If
                If dttemp.Rows.Count > 0 Then
                    Dim drr As DataRow() = dttemp.Select("equip_id='" & row("equip_id") & "'")
                    For idx = 0 To drr.Count() - 1
                        drr(idx)("status_name") = row("status_name")
                        drr(idx)("status") = row("status")
                        drr(idx)("prod_id") = row("prod_id")
                    Next
                End If
            Next
        End If
        dgv.AutoGenerateColumns = False
        dgv.DataSource = dttemp
        For index = 0 To dgv.Rows.Count - 1
            dgv.Rows(index).Selected = False
        Next
        If dgv.CurrentRow.Index > -1 Then
            dgv.Rows(dgv.CurrentRow.Index).Selected = True
        End If
        'For index = 0 To dgv.Rows.Count - 1
        '    If dgv.Rows(index).Cells("equip_id").Value.ToString() = dgvSelectedEquipId Then
        '        'dgv.FirstDisplayedScrollingRowIndex = index
        '        dgv.Rows(index).Selected = True
        '        Exit For
        '    End If
        'Next
    End Sub

    Public Sub initData()
        dttemp.Rows.Clear()
        dtLHJ = objClsHWSetting.selectAllLHJ()
        'Dim dtLhPgd As DataTable = objLhPgd.getLhPgd()
        Dim dtLhPgd As DataTable = objLhPgd.getLHPlan()
        dtxl = New List(Of String)
        lhjLists = String.Empty
        For index = 0 To dtLhPgd.Rows.Count - 1
            Dim lhjs As String = dtLhPgd.Rows(index)("lhj").ToString()
            Dim lhj_list As String() = lhjs.Split(",")
            For inex = 0 To lhj_list.Length - 1
                dtxl.Add(lhj_list(inex))
                lhjLists &= "'" & lhj_list(inex) & "',"
                dttemp.Rows.Add(lhj_list(inex), "", "", "",
                                dtLhPgd.Rows(index)("support_model").ToString(),
                                dtLhPgd.Rows(index)("prod_spec_name").ToString(),
                                dtLhPgd.Rows(index)("sys_id").ToString(),
                                dtLhPgd.Rows(index)("lh_pgd_code").ToString(),
                                dtLhPgd.Rows(index)("jp_type").ToString(),
                                dtLhPgd.Rows(index)("type_value").ToString(),
                                dtLhPgd.Rows(index)("oper_count").ToString(),
                                dtLhPgd.Rows(index)("lhj_finish_count").ToString(),
                                dtLhPgd.Rows(index)("lhj_finish_count_out").ToString())
            Next
        Next
        Dim dvv As DataView = dttemp.DefaultView
        dvv.Sort = "equip_id"
        dttemp = dvv.ToTable()
        If String.IsNullOrEmpty(lhjLists) = False Then
            lhjLists = lhjLists.Substring(0, lhjLists.Length - 1)
        End If
        For index = 0 To dtLHJ.Rows.Count - 1
            If dtxl.Contains(dtLHJ.Rows(index)("equip_id")) = False Then
                dtLHJ.Rows(index).Delete()
            End If
        Next
    End Sub

    Private Sub initAgvWebSite()
        Dim dt As DataTable = objClsSysParam.selectAgvWebSite()
        If dt.Rows.Count = 1 Then
            agv_website = dt.Rows(0)("param_value").ToString()
        End If
    End Sub

    Private Function setAgvNewTask(id As String, prio As String, pick As String, drop As String, agv As String) As String
        Dim msg As String
        Dim interface_msg As String = String.Empty
        If String.IsNullOrEmpty(agv_website) = False Then
            Dim url As String = agv_website & "AGVAPI?do=NewTask&id=" & id & "&prio=" & prio & "&pick=" & pick & "&drop=" & drop & "&agv=" & agv
            Dim httphelper As HttpHelper = New HttpHelper()
            Dim rtn As String = String.Empty
            Try
                rtn = httphelper.HttpGet(url, "")
            Catch ex As Exception
                interface_msg = ex.Message
            End Try
            If String.IsNullOrEmpty(rtn) = False And String.IsNullOrEmpty(interface_msg) Then
                Dim jo As JObject = JsonConvert.DeserializeObject(rtn)
                Dim code As String = jo("code").ToString() '输出0
                msg = jo("msg").ToString() '输出ok
            Else
                msg = "调取agv接口失败，请确保开启AGV服务：" & interface_msg
            End If
        Else
            msg = "请联系工作人员，配置agv网址"
        End If
        Return msg
    End Function

    Private Sub btnRefresh_Click(sender As Object, e As EventArgs) Handles btnRefresh.Click
        initData()
        initDgv()
    End Sub

    Private Sub dgv_MouseClick(sender As Object, e As MouseEventArgs) Handles dgv.MouseClick
        If dgv.Rows.Count > 0 Then
            dgvSelectedEquipId = dgv.Rows(dgv.CurrentRow.Index).Cells("equip_id").Value.ToString()
            dgvSelectedIndex = dgv.CurrentRow.Index
            'dgv.FirstDisplayedScrollingRowIndex = dgv.CurrentRow.Index
            '查询库存信息
            If dgv.Rows(dgv.CurrentRow.Index).Cells("status").Value.ToString().Equals("0") Or dgv.Rows(dgv.CurrentRow.Index).Cells("status").Value.ToString().Equals("3") Then
                Dim dtc As DataTable = objCPCK.getList("0", dgv.Rows(dgv.CurrentRow.Index).Cells("type_value").Value.ToString() & "yz")
                If dtc.Rows.Count > 0 Then
                    If IsDBNull(dtc.Rows(0)("hour_length")) = False Then
                        If Integer.Parse(dtc.Rows(0)("hour_length")) >= 8 Then
                            If dtc.Rows(0)("wh_no").ToString().Contains("A1") Or dtc.Rows(0)("wh_no").ToString().Contains("A3") Or dtc.Rows(0)("wh_no").ToString().Contains("A5") Then
                                txtMsg.Text = "库位：" & dtc.Rows(0)("wh_no") & "在制片（橡胶）车间 请进行AGV上料"
                            Else
                                txtMsg.Text = "库位：" & dtc.Rows(0)("wh_no") & "在成型（硫化）车间 请进行直接入炉"
                            End If
                        Else
                            txtMsg.Text = "库位：" & dtc.Rows(0)("wh_no") & " 在库时长：" & dtc.Rows(0)("hour_length") & " 小时，无法进行出库"
                        End If
                    Else
                        txtMsg.Text = "库位：" & dtc.Rows(0)("wh_no") & " 暂未获取到在库时长，即刚入库，无法进行出库"
                    End If
                Else
                    txtMsg.Text = "预组装仓库中暂时没有此计划支座，请核查"
                End If
            Else
                txtMsg.Text = "此硫化机非空闲状态"
            End If
        End If
    End Sub

    Private Sub FormLH_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
        DBacs.Disconnect()
    End Sub

    Private Sub btnWarehouseYZZH_Click(sender As Object, e As EventArgs) Handles btnWarehouseYZZH.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formWarehouseYZZH As New FormWarehouseYZZH()
            formWarehouseYZZH.usr = cmbOpers.EditValue.ToString()
            formWarehouseYZZH.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！")
        End If
    End Sub

    Private Sub btnMesWorkReportList_Click(sender As Object, e As EventArgs) Handles btnMesWorkReportList.Click
        Dim formMesWorkReportList As New FormMesWorkReportList()
        formMesWorkReportList.ShowDialog(Me)
    End Sub

    Private Sub btnPlan_Click(sender As Object, e As EventArgs) Handles btnPlan.Click
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) = False Then
            Dim formPlan As New FormPlan()
            formPlan.usr = cmbOpers.EditValue.ToString()
            formPlan.ShowDialog(Me)
        Else
            MsgBox("请选择操作用户！", vbYes, "提示")
        End If
    End Sub

    Private Sub btnLhCurrent_Click(sender As Object, e As EventArgs) Handles btnLhCurrent.Click
        Dim formCurrent As New FormCurrent()
        formCurrent.usr = cmbOpers.EditValue.ToString()
        formCurrent.ShowDialog()
    End Sub

    Private Sub btnInto_Click(sender As Object, e As EventArgs) Handles btnInto.Click
        If check() Then
            If dgv.SelectedRows(0).Cells("status").Value.ToString().Equals("0") = False Then
                MsgBox("此硫化机当前状态是非空闲状态，无法进行入炉", vbYes, "提示")
                Exit Sub
            End If
            If dgv.SelectedRows(0).Cells("oper_count").Value.ToString().Equals(dgv.SelectedRows(0).Cells("lhj_finish_count").Value.ToString()) Then
                MsgBox("此硫化机的生产计划入炉数已达到计划数，无法进行入炉", vbYes, "提示")
                Exit Sub
            End If
            If String.IsNullOrEmpty(s_id) Then
                MsgBox("请扫描上层法兰板料框吊篮号", vbYes, "提示")
                Exit Sub
            End If
            If String.IsNullOrEmpty(dlno) Then
                MsgBox("请扫描上层法兰板料框吊篮号", vbYes, "提示")
                Exit Sub
            End If
            If MsgBox("确认要进行入炉吗？", vbYesNo, "提示") = vbYes Then
                Dim prod_id As String = txtBarCodeInto.Text.ToUpper().Trim()
                Dim dtProdInfo As DataTable = objProdInfo.getProdInfoById(prod_id)
                If dtProdInfo.Rows.Count = 1 Then
                    If dtProdInfo.Rows(0)("material_code").ToString().Equals(txt_flb_type.Tag.ToString()) Then
                        If dtProdInfo.Rows(0)("prod_spec_id").ToString().Equals(dgv.SelectedRows(0).Cells("prod_spec_id").Value.ToString()) Then
                            If dtProdInfo.Rows(0)("status").ToString().Equals("4") Then
                                getRbChechedText()
                                Dim rtnt As Integer = objProdInfo.intoLHJ(prod_id, dgv.SelectedRows(0).Cells("equip_id").Value.ToString(),
                                            txt_jstzjl.Value, cmbOpers.EditValue.ToString(), rb_checked_text,
                                            dgv.SelectedRows(0).Cells("sys_id").Value.ToString(),
                                            dgv.SelectedRows(0).Cells("lh_pgd_code").Value.ToString(),
                                            dgv.SelectedRows(0).Cells("prod_spec_id").Value.ToString(), s_id, dlno)
                                If rtnt <> -2 Then
                                    Dim dtlhpgdid As DataTable = objLhPgd.getListById(dgv.SelectedRows(0).Cells("sys_id").Value.ToString())
                                    Dim dtpgdmes As DataTable = objPgdMes.getList(dtlhpgdid.Rows(0)("b_pgd_mes_sys_id").ToString())
                                    Dim b_SysId As String = dtpgdmes.Rows(0)("sys_id").ToString()
                                    Dim b_PgdMesProjectCode As String = dtpgdmes.Rows(0)("mes_project_code").ToString()
                                    Dim b_PgdMes_gx As String = dtpgdmes.Rows(0)("type_value").ToString()
                                    Dim rtn_mes_bg As String = mesbg("1", b_PgdMes_gx, b_PgdMesProjectCode)
                                    Dim jo_bg As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
                                    Dim code_bg As String = jo_bg("code").ToString() '输出0
                                    Dim msg_bg As String = jo_bg("msg").ToString() '输出操作成功
                                    objPgdMesWorkReport.insert("ok", b_SysId, "1", prod_id, cmbOpers.EditValue.ToString(), "3", code_bg)
                                    Dim rtn_mes_rk As String = mesRk("1", warehousecodelh, b_PgdMes_gx, b_PgdMesProjectCode)
                                    Dim jo_rk As JObject = JsonConvert.DeserializeObject(rtn_mes_rk)
                                    Dim code_rk As String = jo_rk("code").ToString() '输出0
                                    Dim msg_rk As String = jo_rk("msg").ToString() '输出操作成功
                                    objLhCkMes.insert("ok", b_SysId, "1", prod_id, cmbOpers.EditValue.ToString(), "3", code_rk)
                                    txtBarCodeInto.Text = String.Empty
                                    txt_jstzjl.Value = 0.00
                                    txt_dl_no.Text = String.Empty
                                    txt_flb_type.Text = String.Empty
                                    txt_which_side.Text = String.Empty
                                    If code_bg.Equals("0") Then
                                        If code_rk.Equals("0") Then
                                            MsgBox("入炉成功，MES报工成功，MES入库成功", vbYes, "提示")
                                        Else
                                            MsgBox("入炉成功，MES报工成功，MES入库失败：" & msg_rk, vbYes, "提示")
                                        End If
                                    Else
                                        If code_rk.Equals("0") Then
                                            MsgBox("入炉成功，MES报工失败：" & msg_bg & "，MES入库成功", vbYes, "提示")
                                        Else
                                            MsgBox("入炉成功，MES报工失败：" & msg_bg & "，MES入库失败：" & msg_rk, vbYes, "提示")
                                        End If
                                    End If
                                Else
                                    MsgBox("入炉失败", vbYes, "提示")
                                End If
                            Else
                                MsgBox("当前标签支座状态非预组装出库状态，无法入炉，请核查", vbYes, "提示")
                            End If
                        Else
                            MsgBox("当前标签支座规格型号与硫化机的计划支座规格型号不一致，无法入炉！", vbYes, "提示")
                        End If
                    Else
                        MsgBox("当前标签的支座上层法兰板规格型号与支座下层法兰板的规格型号不一致，无法入炉！", vbYes, "提示")
                    End If
                Else
                    MsgBox("查询此支座标签号有" & dtProdInfo.Rows.Count.ToString() & "条记录，请核查！", vbYes, "提示")
                End If
            End If
        End If
    End Sub
    Private Function check() As Boolean
        Dim rtn As Boolean = True
        If String.IsNullOrEmpty(cmbOpers.EditValue.ToString()) Then
            MsgBox("请选择操作用户！", vbYes, "提示")
            rtn = False
        End If
        If dgv.SelectedRows.Count <= 0 Then
            MsgBox("请选择硫化机", vbYes, "提示")
            rtn = False
        End If
        If String.IsNullOrEmpty(txtBarCodeInto.Text.Trim()) Then
            MsgBox("请扫描或输入标签号", vbYes, "提示")
            rtn = False
        End If
        Return rtn
    End Function

    Private Function mesbg(oper_num As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """qualifiednumber"":""" & Double.Parse(oper_num) & """," '报工的合格数
        params &= """unqualifiednumber"":""" & 0.00 & """," '报工的不合格数
        params &= """productorname"":""" & productorname & """," '报工产品编码
        params &= """procedurename"":""" & "硫化" & """," '报工工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """unqualifiedReason"":""" & "" & """" '不合格原因
        params &= "}"
        Return httpHelper.HttpPostWebService("dispatchingOrderBG", params)
    End Function

    Private Function mesRk(oper_num As String, warehousecode As String, productorname As String, b_gtPgdMesProjectCode As String) As String
        Dim params As String = "{"
        params &= """project_code"":""" & b_gtPgdMesProjectCode & """," 'mes合批号
        params &= """rkcount"":""" & Double.Parse(oper_num) & """," '入库数量
        params &= """warehousecode"":""" & warehousecode & """," '入库仓库编码
        params &= """productorname"":""" & productorname & """," '产品编码
        params &= """procedurename"":""" & "硫化" & """," '执行工序
        params &= """compid"":""0de02b54-4e55-4dbf-ab89-f785df5f405c""," '公司id
        params &= """departmentid"":""TS1103104""" '部门id
        params &= "}"
        Return httpHelper.HttpPostWebService("productFinishedToWarehouse", params)
    End Function

    Private Sub btnOut_Click(sender As Object, e As EventArgs) Handles btnOut.Click
        Out(0)
    End Sub

    Private Sub Out(ty As Integer)
        If check() Then
            If dgv.SelectedRows(0).Cells("status").Value.ToString().Equals("3") = False Then
                MsgBox("此硫化机当前状态是非即将出炉的状态，无法进行出炉")
                Exit Sub
            End If
            Dim prod_id As String = txtBarCodeInto.Text.ToUpper().Trim()
            If dgv.SelectedRows(0).Cells("prod_id").Value.ToString().Equals(prod_id) = False Then
                MsgBox("出炉标签号与入炉标签号不一致，请核查！")
                Exit Sub
            End If
            Dim dtpInfo As DataTable = objProdInfo.getProdInfoById(prod_id)
            If dtpInfo.Rows(0)("status").ToString().Equals("5") = False Then
                MsgBox("当前扫描标签号状态不处于硫化中，请核查")
                Exit Sub
            End If
            If MsgBox("请确保已正确输入溢胶量，确认要进行出炉吗？", vbYesNo, "提示") = vbYes Then
                getRbChechedText()
                Dim rtnt As Integer = objProdInfo.outLHJ(prod_id, dgv.SelectedRows(0).Cells("equip_id").Value.ToString(),
                                                         cmbOpers.EditValue.ToString(), rb_checked_text, txt_yjzl.Value)
                If rtnt <> -2 Then
                    If ty.Equals(1) Then ' 出炉AGV去脱模
                        toTM()
                    End If
                    txtBarCodeInto.Text = String.Empty
                    txt_yjzl.Value = 0.00
                    If ty.Equals(0) Then '只出炉
                        MsgBox("出炉成功！", vbYes, "提示")
                    End If
                Else
                    MsgBox("出炉失败", vbYes, "提示")
                End If
            End If
        End If
    End Sub

    Private Sub toTM()
        If dgv.SelectedRows.Count <= 0 Then
            MsgBox("请选择硫化机", vbYes, "提示")
            Exit Sub
        End If
        If String.IsNullOrEmpty(txtBarCodeInto.Text.Trim()) Then
            MsgBox("请扫描或输入标签号", vbYes, "提示")
            Exit Sub
        End If
        Dim taskTM_id As String = Now.ToString("yyyyMMddHHmmss")
        '创建一条要脱模的数据
        objTaskTM.insert(taskTM_id, dgv.SelectedRows(0).Cells("equip_id").Value.ToString(), txtBarCodeInto.Text.ToUpper().Trim())
        Dim dtAgvRoute As DataTable = objAgvRoute.SelectAgvRouteByLHJ(dgv.SelectedRows(0).Cells("equip_id").Value.ToString())
        Dim msg As String = setAgvNewTask(taskTM_id,
                        "0",
                        dtAgvRoute.Rows(0)("lhj_point").ToString(),
                        dtAgvRoute.Rows(0)("tm_point_front").ToString(),
                        "0")
        If msg.Equals("ok") Then
            MsgBox("出炉去脱模成功！", vbYes, "提示")
        Else
            MsgBox("出炉成功，去脱模失败，失败信息：" & msg, vbYes, "提示")
        End If
    End Sub

    Private Sub btnOutAndTm_Click(sender As Object, e As EventArgs) Handles btnOutAndTm.Click
        'Dim rtn_mes_bg As String = mesbg("1", "02.02.009", "接口入库测试428-1")
        'Dim jo As JObject = JsonConvert.DeserializeObject(rtn_mes_bg)
        'Dim code As String = jo("code").ToString() '输出0
        'Dim msg As String = jo("msg").ToString() '输出操作成功
        Out(1)
    End Sub

    Private Sub txt_MouseDown(sender As Object, e As MouseEventArgs) Handles txt_jstzjl.MouseDown, txt_yjzl.MouseDown
        UcDigitalKeyboard1 = New ucDigitalKeyboard
        If sender.MaskBox.Mask.EditMask = "d" Then
            UcDigitalKeyboard1.B_01 = False
        Else
            UcDigitalKeyboard1.B_01 = True
        End If

        If sender.GetType().Name.Equals("TextEdit") Then
            UcDigitalKeyboard1.B_02 = True
        Else
            UcDigitalKeyboard1.B_02 = False
        End If

        UcDigitalKeyboard1.Txt = sender

        Me.Controls.Add(UcDigitalKeyboard1)
        UcDigitalKeyboard1.Show()
        UcDigitalKeyboard1.BringToFront()
        UcDigitalKeyboard1.Focus()
    End Sub

    Private Sub ucdigitalkeyboard1_leave(sender As Object, e As EventArgs) Handles UcDigitalKeyboard1.Leave
        UcDigitalKeyboard1.Dispose()
    End Sub

    Private Sub btnArriveLHJ_Click(sender As Object, e As EventArgs) Handles btnArriveLHJ.Click
        If dgv.SelectedRows.Count <= 0 Then
            MsgBox("请选择硫化机", vbYes, "提示")
            Exit Sub
        End If
        If MsgBox("确认要到此硫化机吗？", vbYesNo, "提示") = vbYes Then
            'Dim dtAgvRoute As DataTable = objAgvRoute.SelectAgvRouteByLHJ("800T-01")
            Dim dtAgvRoute As DataTable = objAgvRoute.SelectAgvRouteByLHJ(dgv.SelectedRows(0).Cells("equip_id").Value.ToString())

            Dim dr As DataRow()
            If dtAgvRoute.Rows.Count = 1 Then
                dr = dtAgvRoute.Select("")
            End If
            If dtAgvRoute.Rows.Count = 2 Then
                If dgv.SelectedRows(0).Cells("prod_spec_name").Value.ToString().Contains("700") Then
                    dr = dtAgvRoute.Select("type='A'")
                End If
                If dgv.SelectedRows(0).Cells("prod_spec_name").Value.ToString().Contains("800") Then
                    dr = dtAgvRoute.Select("type='B'")
                End If
            End If
            Dim msg As String = setAgvNewTask(Now.ToString(), "0",
                                    dr(0)("prod_point").ToString(),
                                    dr(0)("lhj_point").ToString(),
                                    "0")
            If msg.Equals("ok") Then
                MsgBox("到硫化机成功", vbYes, "提示")
            Else
                MsgBox("到硫化机失败，失败信息：" & msg, vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub btnArriveTMJ_Click(sender As Object, e As EventArgs) Handles btnArriveTMJ.Click
        If String.IsNullOrEmpty(txtBarCodeInto.Text.Trim()) Then
            MsgBox("请扫描或输入标签号", vbYes, "提示")
            Exit Sub
        End If
        Dim dtTaskTm As DataTable = objTaskTM.getList(txtBarCodeInto.Text.Trim())
        If dtTaskTm.Rows.Count >= 1 Then
            Dim dtAgvRoute As DataTable = objAgvRoute.SelectAgvRouteByLHJ(dtTaskTm.Rows(0)("lhj").ToString())
            Dim msg As String = setAgvNewTask(dtTaskTm.Rows(0)("task_id").ToString(),
                            "0",
                            dtAgvRoute.Rows(0)("lhj_point").ToString(),
                            dtAgvRoute.Rows(0)("tm_point_front").ToString(),
                            dtTaskTm.Rows(0)("agv").ToString())
            If msg.Equals("ok") Then
                MsgBox("AGV去脱模成功！", vbYes, "提示")
                txtBarCodeInto.Text = String.Empty
            Else
                MsgBox("AGV去脱模失败！失败信息：" & msg, vbYes, "提示")
            End If
        Else
            If dtTaskTm.Rows.Count = 0 Then
                MsgBox("未查询到此支座的脱模任务！请先进行出炉！", vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub btnArriveTMJBack_Click(sender As Object, e As EventArgs) Handles btnArriveTMJBack.Click
        If dgv.SelectedRows.Count <= 0 Then
            MsgBox("请选择硫化机", vbYes, "提示")
            Exit Sub
        End If
        If MsgBox("确认要到脱模机后吗？", vbYesNo, "提示") = vbYes Then
            Dim dtAgvRoute As DataTable = objAgvRoute.SelectAgvRouteByLHJ(dgv.SelectedRows(0).Cells("equip_id").Value.ToString())
            Dim msg As String = setAgvNewTask(Now.ToString(), "0",
                                    dtAgvRoute.Rows(0)("lhj_point").ToString(),
                                    dtAgvRoute.Rows(0)("tm_point_n").ToString(),
                                    "0")
            If msg.Equals("ok") Then
                MsgBox("到脱模机后成功", vbYes, "提示")
            Else
                MsgBox("到脱模机后失败，失败信息：" & msg, vbYes, "提示")
            End If
        End If
    End Sub

    Private Sub rbKX_CheckedChanged(sender As Object, e As EventArgs) Handles rbKX.CheckedChanged
        If MsgBox("确认要修改此硫化机的状态吗？", vbYesNo, "提示") = vbYes Then
            If rbKX.Checked Then
                xxx("0", "789")
            End If
        Else
            rbKX.Checked = False
        End If
    End Sub

    Private Sub rbDCL_CheckedChanged(sender As Object, e As EventArgs) Handles rbDCL.CheckedChanged
        If MsgBox("确认要修改此硫化机的状态吗？", vbYesNo, "提示") = vbYes Then
            If rbDCL.Checked Then
                xxx("3", "3")
            End If
        Else
            rbDCL.Checked = False
        End If
    End Sub

    Private Sub xxx(status As String, lm As String)
        If dgv.SelectedRows.Count > 0 Then
            Dim lhj As String = dgv.SelectedRows(0).Cells("equip_id").Value.ToString()
            Dim rtn As Integer = objHWSetting.SetLHJstatus(status, lm, lhj)
            objLog.insert("硫化机", "硫化机状态", lhj & "状态改为：" & status)
        End If
    End Sub

    Private Sub txt_dl_no_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txt_dl_no.KeyPress
        If String.IsNullOrEmpty(txt_dl_no.Text.Trim) = False Then
            If e.KeyChar = vbCr Then
                txt_dl_no.Text = txt_dl_no.Text.Trim()
                getDlNoFlb()
            End If
        End If
    End Sub

    Private Sub getDlNoFlb()
        flb_info = objFlbInfo.getFlbInfoByDlNo(txt_dl_no.Text.Trim)
        With flb_info
            If .Rows.Count > 0 Then
                s_id = .Rows(0)("sys_id")
                dlno = .Rows(0)("dl_no")
                If .Rows(0)("which_side") = "0" Then
                    txt_which_side.Text = "朝上"
                Else
                    txt_which_side.Text = "朝下"
                End If
                txt_flb_type.Text = .Rows(0)("flb_type")
                If DBNull.Value.Equals(.Rows(0)("material_code")) Then
                    txt_flb_type.Tag = .Rows(0)("flb_material_code")
                Else
                    txt_flb_type.Tag = .Rows(0)("material_code")
                End If
                txt_flb_count.Text = CInt(.Rows(0)("piece_num")) - CInt(.Rows(0)("used_piece_num"))
            Else
                MsgBox("未检索到法兰板吊篮号")
            End If
        End With
    End Sub

    Private Sub txt_dl_no_TextChanged(sender As Object, e As EventArgs) Handles txt_dl_no.TextChanged
        txt_flb_type.Text = String.Empty
        txt_flb_count.Text = String.Empty
        txt_which_side.Text = String.Empty
    End Sub

    Private Sub btnChangeDirection_Click(sender As Object, e As EventArgs) Handles btnChangeDirection.Click
        If String.IsNullOrEmpty(txt_which_side.Text.Trim()) = False Then
            If txt_which_side.Text.Trim().Equals("朝上") Then
                Dim rtn1 As Integer = objFlbInfo.updateWhichSideBySysId("1", s_id)
                If rtn1 <> -2 Then
                    MsgBox("调整方向成功", vbYes, "提示")
                Else
                    MsgBox("调整方向失败", vbYes, "提示")
                End If
                getDlNoFlb()
            Else
                Dim rtn0 As Integer = objFlbInfo.updateWhichSideBySysId("0", s_id)
                If rtn0 <> -2 Then
                    MsgBox("调整方向成功", vbYes, "提示")
                Else
                    MsgBox("调整方向失败", vbYes, "提示")
                End If
                getDlNoFlb()
            End If
        Else
            MsgBox("请扫描或输入法兰板吊篮号！", vbYes, "提示")
        End If
    End Sub

    Private Sub btnUpdateFlbInfo_Click(sender As Object, e As EventArgs) Handles btnUpdateFlbInfo.Click
        Dim formFlbInfoList As New FormFlbInfoList()
        formFlbInfoList.ShowDialog()
    End Sub

    Private Sub btnFlbInfoListUnused_Click(sender As Object, e As EventArgs) Handles btnFlbInfoListUnused.Click
        Dim formFlbInfoListUnused As New FormFlbInfoListUnused()
        formFlbInfoListUnused.ShowDialog()
    End Sub

    Private Sub btnChangeLhj_Click(sender As Object, e As EventArgs) Handles btnChangeLhj.Click
        Dim formChangeLhj As New FormChangeLhj()
        formChangeLhj.ShowDialog()
    End Sub
End Class